package com.example.smartcity_poverty.ui.PersonalCenter


import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.PersonalCenter.activity.LoginActivity
import com.example.smartcity_poverty.ui.PersonalCenter.adapter.PersonHomeAdapter
import kotlinx.android.synthetic.main.fragment_notifications.*


/**
 * 底部导航栏 - 个人中心
 * custom control
 */
class PersonFragment : Fragment() {
    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_notifications, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setListAdapter()
        setEvent()
    }

    /**
     * 处理点击事件
     */
    private fun setEvent() {
        personOk.setOnClickListener {
            if (personOk.text.toString() == "登录"){
                startActivity(Intent(context, LoginActivity::class.java))
            }

            if (personOk.text == "退出"){
                alert()
            }
        }
    }

    /**
     * Alert
     */
    private fun alert(){
        AlertDialog.Builder(context).apply {
            setTitle("提示")
            setMessage("确定要退出！")
            setPositiveButton("确定"){_, _ ->
                context.getSharedPreferences("token", Context.MODE_PRIVATE)
                    ?.edit()
                    ?.putString("token", "")
                    ?.putBoolean("user", false)
                    ?.apply()
                onStart()
            }

            setNegativeButton("取消"){_, _ ->

            }
            show()
        }
    }

    /**
     * 配置列表
     */
    private fun setListAdapter() {
        personRecycler.apply {
            this.layoutManager = LinearLayoutManager(context)
            this.adapter = PersonHomeAdapter(ArrayList<String>().apply {
                this.add("个人信息")
                this.add("订单列表")
                this.add("修改密码")
                this.add("意见反馈")
            }, R.layout.re_item_person)
        }
    }

    /**
     * 判断用户是否已经登录
     */
    override fun onStart() {
        super.onStart()
        context?.getSharedPreferences("token", Context.MODE_PRIVATE).apply {
            val token = this?.getString("token", "").toString()
            if (this?.getBoolean("user", false) == true){
                // 开启一个协程来获取用户数据
                coroutine {
                    api.loginAll(token).apply {
                        if (this.code == 200){
                            activity?.runOnUiThread {
                                personName.text = this.user?.nickName
                                personOk.text = "退出"
                            }
                        }else "加载用户信息失败！".show()
                    }
                }
            }else{
                personOk.text = "登录"
                personName.text = "您还未登录！"
            }
        }
    }
}